<script setup>
//import { artEditChannelsService } from '@/api/article'
//import { artAddChannelsService } from '@/api/article'
import { ref } from 'vue'
// import AnnouncementApi from '@/service/api/announcement/announcement'
import { ElMessage } from 'element-plus'

const dialogVisible = ref(false)

const formModel = ref({
  title: '',
  text: '',
  publisher: '',
  createdAt: Date.now(),
  content: '',
  show: 0
})

const rules = {
  publisher: [
    { required: true, message: '请输入公告人', trigger: 'blur' },
    {
      pattern: /^\S{1,20}$/,
      message: '公告人不能为空',
      trigger: 'blur'
    }
  ],
  title: [
    { required: true, message: '请输入公告标题', trigger: 'blur' }, //最后一句是指失焦时校验,change是改变校验
    {
      pattern: /^\S{1,20}$/,
      message: '标题不能为空',
      trigger: 'blur'
    }
  ],
  content: [
    { required: true, message: '请输入公告内容', trigger: 'blur' },
    {
      //正则校验
      pattern: /^\S{1,1000}$/,
      message: '内容不能为空',
      trigger: 'blur'
    }
  ],
  show: [{ required: true, message: '请选择是否展示', trigger: 'blur' }]
}

const open = (row) => {
  //设置编辑文章类型时显示已有内容，即将弹窗表内容设为当前行的内容，当点击当前行的编辑按钮时触发
  dialogVisible.value = true
  formModel.value = { ...row }
}

const emits = defineEmits(['success'])
const formRef = ref()

defineExpose({
  open
})
</script>
<template>
  <!--弹窗页-->
  <el-dialog v-model="dialogVisible" :title="'添加公告'" width="40%" height="100">
    <el-form :model="formModel" :rules="rules" ref="formRef">
      <el-form-item prop="publisher" label="公告人">
        <el-input v-model="formModel.publisher" placeholder="请输入公告人姓名"></el-input>
      </el-form-item>
      <el-form-item prop="title" label="公告标题">
        <el-input v-model="formModel.title" placeholder="请输入公告标题"></el-input>
      </el-form-item>
      <el-form-item prop="content" label="公告内容"
        ><!--label 加到item上-->
        <el-input
          v-model="formModel.content"
          placeholder="请输入公告内容"
          type="textarea"
        ></el-input>
      </el-form-item>
      <el-form-item prop="show" label="是否展示">
        <el-switch v-model="formModel.show" active-value="1" inactive-value="0"></el-switch>
      </el-form-item>
    </el-form>

    <template #footer>
      <div>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary"> 确认 </el-button>
      </div>
    </template>
  </el-dialog>
</template>
